/**
 * Title : jFamilyTree application
 * Description: jFamilyTree application
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree;

import java.awt.Component;
import java.awt.Image;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JFrame;

/**
 * Application
 * 
 * @author Bernard Bou
 */
public class Application extends JFrame implements IContext
{
	private static final long serialVersionUID = 1L;

	/**
	 * Arguments
	 */
	private String[] theArgs;

	/**
	 * Constructor
	 * 
	 * @param theseArgs
	 *        arguments
	 */
	public Application(String[] theseArgs)
	{
		// System.out.println("CLASSPATH=<" + System.getProperty("java.class.path", ".") + ">");
		theArgs = theseArgs;
		setTitle("jFamilyTree");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		IWidget thisWidget = new Widget(this);
		thisWidget.init();
		getContentPane().add((Component) thisWidget);

		pack();
		setVisible(true);
	}

	// C O N T E X T

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.component.Context#getBase()
	 */
	public URL getBase()
	{
		// base parameter
		String thisBase = getParameter("base");
		String thisURLString = thisBase != null ? thisBase : System.getProperty("user.dir");

		// tail
		if (!thisURLString.endsWith("/"))
			thisURLString += "/";

		// make
		try
		{
			return new URL(thisURLString);
		}
		catch (MalformedURLException e)
		{
			// make from folder
			try
			{
				File thisFolder = new File(thisURLString);
				return thisFolder.toURI().toURL();
			}
			catch (MalformedURLException thisException)
			{
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.Context#getHtmlDocumentBase()
	 */
	public URL getHtmlDocumentBase()
	{
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.Context#getImagesBase()
	 */
	public URL getImagesBase()
	{
		URL thisBase = null;
		String thisImageBase = getParameter("images");
		if (thisImageBase != null && !thisImageBase.isEmpty())
		{
			// 1-use 'images' parameter as full-fledged url
			try
			{
				return new URL(thisImageBase);
			}
			catch (MalformedURLException e)
			{
			}

			if (!thisImageBase.endsWith("/"))
				thisImageBase += "/";

			// 2-use 'images' parameter as relative to base
			thisBase = getBase();
			try
			{
				return new URL(thisBase, thisImageBase);
			}
			catch (MalformedURLException thisException)
			{
			}
		}

		if (thisBase == null)
			thisBase = getBase();

		// 3-use set 'images/' path as relative to base
		try
		{
			return new URL(thisBase, "images/");
		}
		catch (MalformedURLException thisException)
		{
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.component.Context#getImage(java.net.URL)
	 */
	public Image getImage(URL thisURL)
	{
		return java.awt.Toolkit.getDefaultToolkit().getImage(thisURL);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.component.Context#getParameter(java.lang.String)
	 */
	public String getParameter(String thisName)
	{
		// param1=<val> param2=<"val with spaces"> ...
		if (theArgs == null)
			return null;
		for (String thisParam : theArgs)
			if (thisParam.startsWith(thisName + "="))
			{
				String thisValue = thisParam.substring(thisName.length() + 1);
				if (thisValue.startsWith("\""))
				{
					thisValue = thisValue.substring(1);
					if (thisValue.endsWith("\""))
						thisValue = thisValue.substring(0, thisValue.length() - 1);
				}
				return thisValue;
			}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.component.Context#linkTo(java.lang.String)
	 */
	public boolean linkTo(String thisLinkUrl, String thisLinkTarget)
	{
		System.out.println("Link to: " + thisLinkUrl);
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.component.Context#showStatus(java.lang.String)
	 */
	public void showStatus(String thisString)
	{
		System.out.println("Status: " + thisString);
	}

	/**
	 * Main
	 * 
	 * @param theseArgs
	 *        arguments
	 */
	public static void main(String[] theseArgs)
	{
		new Application(theseArgs);
	}
}
